<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
        <el-row :gutter="20">
            <el-col :span="10" :offset="3" >
           <el-input placeholder="请输入内容" v-model="input3" class="input-with-select">
                <el-select style="width:130px" v-model="select" slot="prepend" placeholder="请选择">
                    <el-option v-for="item in selectOptions"
                    :key="item.value" 
                    :label="item.lable"
                     :value="item.value"></el-option>
                </el-select>
                <el-button @click="search" slot="append" icon="el-icon-search" >搜索</el-button>
            </el-input>
          </el-col>

        </el-row>
      <el-table :data="tableData" highlight-current-row style="width: 100%">
        <el-table-column type="id" property="id" width="100" label="ID"> </el-table-column>
        <el-table-column property="nickName" label="网名" width="220">
        </el-table-column>
        <el-table-column property="phNum" label="电话" width="220">
        </el-table-column>
        <el-table-column property="province" label="省份"> </el-table-column>
        <el-table-column property="city" label="城市"> </el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" width="30" height="30" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="Pagination" style="text-align: left; margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
       <!-- 修改提示框 -->
      <el-dialog title="修改用户信息" v-model="dialogFormVisible">
        <div slot="footer">
          <el-form :model="selectTable">
            <el-form-item label="用户昵称" label-width="100px">
              <el-input
                v-model="selectTable.nickName"
                auto-complete="off"
              ></el-input>
            </el-form-item>
            <el-form-item  label="电话" label-width="100px">
              <el-input  v-model="selectTable.phNum"></el-input>
            </el-form-item>
            <el-form-item label="省" label-width="100px">
              <el-input v-model="selectTable.province"></el-input>
            </el-form-item>
            <el-form-item label="城市" label-width="100px">
              <el-input v-model="selectTable.city"></el-input>
            </el-form-item>
            <el-form-item label="姓名" label-width="100px">
              <el-input v-model="selectTable.name"></el-input>
            </el-form-item>
             <el-form-item label="身份证" label-width="100px">
              <el-input v-model="selectTable.idNum"></el-input>
            </el-form-item>
            <el-form-item label="密码" label-width="100px">
              <el-input v-model="selectTable.pwd"></el-input>
            </el-form-item>

             <el-form-item label="头像" label-width="100px">
                <el-row type="flex" >    
                    <el-col :span="3">             
                            <img
                            style="width: 100px; height: 100px"
                            :src="selectTable.avatar"
                            fit="fit"></img>
    
                    </el-col> 
               

                </el-row>
                <el-row type="flex" >    
                    <el-col :span="3">  
                             <el-upload class="avatar-uploader" action="http://localhost:8090/tourism/user/uploadAvatar" 
                                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                                 :data="{id:selectTable.id}"
                                :show-file-list="false"
                            >           
                           <el-button type="primary" >更换头像</el-button>
                             </el-upload>
    
                    </el-col> 
                </el-row>
                

            </el-form-item>
       
            
          </el-form>
                 
           
         
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateShop">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import axios from "axios";
import { getUserList,getUserListBypageByLike,updateUser ,deleteUserById,getUserListBypage} from "@/api/user";
import { getSceneryById } from "@/api/net";
export default {
  data() {
    return {
      tableData: [
        {
          nickName: "2016-05-02",
          phNum: "王小虎",
          province: "江西",
          city: "南昌",
          avatar:
            "https://thirdwx.qlogo.cn/mmopen/vi_32/VMWTEwwcHqfCD56TkJiaVcIoNm4QX1mxwhwts4kWBe6rrNsG75csjE1x4ibmSH1BNLG58gWj9RyQLhaunubapfZw/132",
          name: "",
          idNum: "",
        },
      ],
      list: [],
      currentRow: null,
      headImageFile:'',
      offset: 0,
      limit: 10,
      count: 0,
      currentPage: 1,
      dialogFormVisible:false,
      selectTable:{},
      input3:'',
      select:1,
      selectOptions: [{
        lable:"用户名",value:"1"
      },{
        lable:"电话",value:"2"
      }]
    };
  },
  components: {
    headTop,
  },
  created() {
    this.initData();
   
    this.select = this.selectOptions[0].value
 
  },
  methods: {
    async initData() {
      try {
        // axios.get("http://localhost:8090/tourism/scenery/10").then(res=>{
        //    console.log(res.data);
        // })

        this.getList();
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    handleSizeChange(val) {
      this.limit = val;
      console.log(`每页 ${val} 条`);
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      console.log(this.currentPage, this.offset);
      this.getList();
    },


    async getList() {
      console.log(this.currentPage,this.limit);
      const res = await getUserListBypage(this.currentPage,this.limit);
      if(res&&res.code>=0){
        this.tableData  = res.data.rows;
        this.count = res.data.totalCount
      }else{
        console.log(res.message);
      }   

    },

     async handleDelete(index, row) {
      try {
       
            const res = await deleteUserById(row.id);
            if (res.code >= 0) {
            this.$message({
                type: "success",
                message: "删除成功",
            });
            this.tableData.splice(index, 1);
            this.getList()
            } else {
            throw new Error(res.message);
            }
      } catch (err) {
        this.$message({
          type: "error",
          message: err.message,
        });
      }
    },
    async handleEdit(index, row) {
 
      //index 就是第几个，可以从scenery直接取
      console.log("handleEdit(index, row)", index, row);
  
      this.selectTable = row;
    
      //   this.addr = ;
      this.dialogFormVisible = true;
    },
    async search(){
        //内容，类型
        console.log("input3",this.input3);
        console.log("select",this.select);
        if(this.input3==''){
            this.getList()
            return;
        }
        let data={}
        
            data = {info:this.input3,type:this.select}
        const res = await getUserListBypageByLike(this.currentPage,this.limit,data);
        if(res&&res.code>=0){
            this.tableData  = res.data.rows;
            this.count = res.data.totalCount
        }else{
            console.log(res.message);
        }   

    },
    //跟新用户信息
    async  updateShop(){
        console.log(this.selectTable);
        let res= await updateUser(this.selectTable)
        if(res.code>=0){
             this.$message.success(res.message)
           
        }else
            this.$message.error(res.message);

         this.dialogUploadPicVisible=false;
    },
    handleAvatarSuccess(res, file) {
        this.$message({
          message: '修改成功',
          type:"success"
        });
        console.log(res);
      
     
        this.selectTable.avatar =  res.data.avatar
       
      },
      beforeAvatarUpload(file) {
        
        const isJPG = file.type === 'image/jpeg'||file.type=='image/jpg'||file.type=='png';
        const isLt2M = file.size / 1024 / 1024 < 1;
 
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG ,PNG,JPEG格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 1MB!');
        }
        return isJPG && isLt2M;
      }

  },
};
</script>

<style lang="less">
@import "../style/mixin";
.table_container {
  padding: 20px;
}
.avatar{
    flex-flow: column;
}
</style>
